package com.jzqf.harmonydemo.utils;

import ohos.aafwk.ability.AbilitySlice;

import java.lang.ref.WeakReference;
import java.util.ArrayList;

/**
 * Ability页面管理
 *
 * @author LiuWeiHao
 * 2021/5/12 9:46
 */
public class AbilityManager {
    private static ArrayList<WeakReference<AbilitySlice>> abilityStack = new ArrayList<>();

    public static void addActivity(AbilitySlice abilitySlice) {
        WeakReference<AbilitySlice> act = new WeakReference<>(abilitySlice);
        if (!abilityStack.contains(act)) {
            abilityStack.add(act);
        }
    }

    /**
     * 退出应用
     */
    public static void exitApplication() {
        for (WeakReference<AbilitySlice> ability : abilityStack) {
            if (ability != null && ability.get() != null) {
                ability.get().terminate();
            }
        }
        abilityStack.clear();
    }

    public static void removeActivity(AbilitySlice ability) {
        WeakReference<AbilitySlice> act = new WeakReference<>(ability);
        abilityStack.remove(act);
    }


}
